package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.ProductSkuEntity;
import com.chushouya.product.dao.mapper.ProductSkuEntityMapper;
import com.chushouya.product.dto.admin.productsku.ProductSkuQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class ProductSkuRepository extends BaseRepository<ProductSkuEntityMapper, ProductSkuEntity, Long> {
    
    public List<ProductSkuEntity> selectList(ProductSkuQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<ProductSkuEntity> selectPageResult(ProductSkuQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public ProductSkuEntity selectOne(ProductSkuQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(ProductSkuQuery query) {
        Example example = new Example(ProductSkuEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getSkuId())) {
            criteria.andEqualTo("skuId", query.getSkuId());
        }
        if (Objects.nonNull(query.getProductId())) {
            criteria.andEqualTo("productId", query.getProductId());
        }
        if (Strings.isNotEmpty(query.getSkuCode())) {
            criteria.andEqualTo("skuAttrCode", query.getSkuCode());
        }
        if (Strings.isNotEmpty(query.getSkuName())) {
            criteria.andLike("skuName", query.getSkuName());
        }
        if (Objects.nonNull(query.getBasePriceMin())) {
            criteria.andGreaterThanOrEqualTo("basePrice", query.getBasePriceMin());
        }
        if (Objects.nonNull(query.getBasePriceMax())) {
            criteria.andLessThanOrEqualTo("basePrice", query.getBasePriceMax());
        }
        if (Objects.nonNull(query.getCalcType())) {
            criteria.andEqualTo("calcType", query.getCalcType());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getDelFlag())) {
            criteria.andEqualTo("delFlag", query.getDelFlag());
        } else {
            criteria.andEqualTo("delFlag", 0);
        }
        if (Strings.isNotEmpty(query.getHandleUser())) {
            criteria.andEqualTo("handleUser", query.getHandleUser());
        }
        if (Objects.nonNull(query.getCreateTimeStart())) {
            criteria.andGreaterThanOrEqualTo("createTime", query.getCreateTimeStart());
        }
        if (Objects.nonNull(query.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", query.getCreateTimeEnd());
        }
        if (Objects.nonNull(query.getUpdateTimeStart())) {
            criteria.andGreaterThanOrEqualTo("updateTime", query.getUpdateTimeStart());
        }
        if (Objects.nonNull(query.getUpdateTimeEnd())) {
            criteria.andLessThanOrEqualTo("updateTime", query.getUpdateTimeEnd());
        }
        example.orderBy("sort").asc();
        return example;
    }
    
    public List<ProductSkuEntity> selectListByProductId(Long productId) {
        ProductSkuQuery query = new ProductSkuQuery();
        query.setProductId(productId);
        return selectList(query);
    }
    
    public List<ProductSkuEntity> selectListByProductIds(List<Long> productIds) {
        if (Lists.isEmpty(productIds)) {
            return Lists.newArrayList();
        }
        Example example = new Example(ProductSkuEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("productId", productIds);
        criteria.andEqualTo("delFlag", 0);
        example.orderBy("sort").asc();
        return this.selectByExample(example);
    }
    
    public ProductSkuEntity selectBySkuCode(String skuCode) {
        ProductSkuQuery query = new ProductSkuQuery();
        query.setSkuCode(skuCode);
        return selectOne(query);
    }
    
    /**
     * 批量插入SKU
     */
    public void batchInsert(List<ProductSkuEntity> skuEntities) {
        if (Lists.isEmpty(skuEntities)) {
            return;
        }
        
        // 分批插入，每批100条记录
        int batchSize = 100;
        for (int i = 0; i < skuEntities.size(); i += batchSize) {
            int endIndex = Math.min(i + batchSize, skuEntities.size());
            List<ProductSkuEntity> batchList = skuEntities.subList(i, endIndex);
            
            // 使用tk.mybatis的insertList方法进行批量插入
            this.insertList(batchList);
        }
    }
    
    /**
     * 根据商品ID删除SKU数据
     */
    public void deleteByProductId(Long productId) {
        if (Objects.isNull(productId)) {
            return;
        }
        
        Example example = new Example(ProductSkuEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        
        this.deleteByExample(example);
    }
}
